package com.example.multithread.communication;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Exchanger;

/**
 * @ClassName ExechangerDemo
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2020/5/14 1:43 PM
 * @Version 1.0
 */
public class ExechangerDemo {
	private final static Logger log = LoggerFactory.getLogger(ExechangerDemo.class);
	public static void main(String[] args) throws InterruptedException {
		Exchanger<String> exchanger = new Exchanger<>();
		new Thread(() -> {
			try {
				log.info("当前线程时线程A,{}", exchanger.exchange("我拿到的是线程A给我的消息"));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}).start();

		new Thread(() -> {
			try {
				log.info("当前线程时线程B,{}", exchanger.exchange("我拿到的是线程B给我的消息"));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}).start();

		Thread.sleep(1000);
	}
}
